//
//  OrderListVC.m
//  GasCome
//
//  Created by zfc on 2020/4/9.
//  Copyright © 2020 zfc. All rights reserved.
//

#import "OrderListVC.h"
#import "OrderCommonCell.h"
//#import "OrderPayVC.h"
//#import "OrderWaitPayVC.h"
//#import "OrderDetailVC.h"
#import "OrderRepairCell.h"
//#import "OrderRepairDetailVC.h"
@interface OrderListVC () <CellClickDetegate, OrderRepairCellDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@end

@implementation OrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.dataList = @[@"1",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2"].mutableCopy;
    [self setUI];
}

- (void) setUI {
    [self.tableView registerClass:[OrderCommonCell class] forCellReuseIdentifier:@"OrderCommonCell"];
    [self.tableView registerClass:[OrderRepairCell class] forCellReuseIdentifier:@"OrderRepairCell"];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.isAddFooter = YES;
}


- (void) fetchData {
    NSString *url = @"/api/tiny-shop/v1/member/order/index";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @(self.page);
    params[@"synthesize_status"] = self.synthesize_status;
    [[NetWorksTools shared] requestWithMethod:GET url:url params:params successBlock:^(NSDictionary * _Nonnull response) {
        [self endRefresh];
        NSArray *arr = response[@"data"];
        if (self.page == 1) {
            [self.dataList removeAllObjects];
        } else {
            if (arr.count == 0) {
                self.page = self.page - 1;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.dataList addObjectsFromArray:arr];
        [self.tableView reloadData];
    } failureBlock:^(NSError * _Nonnull error) {
        [self endRefresh];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataList[indexPath.row];
    NSInteger orderType = [dic[@"order_type"] integerValue];
    if (orderType == 10) {
        OrderRepairCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderRepairCell" forIndexPath:indexPath];
        cell.delegate = self;
        cell.orderDic = dic;
        return cell;
    }
    OrderCommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCommonCell" forIndexPath:indexPath];
    cell.delegate = self;
    cell.orderDic = dic;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataList[indexPath.row];
    NSInteger orderType = [dic[@"order_type"] integerValue];
    if (orderType == 10) {
        return UITableViewAutomaticDimension;
    }
    
    if (dic[@"product"]) {
        NSArray *product = dic[@"product"];
           NSInteger type = [dic[@"order_status"] integerValue];
           if (type == 0 || type == -4) {
               return 150+56*product.count;
           } else {
               return 90+56*product.count;
           }
    }
    return 100;
}

/*
//该方法商品cell无法点击 暂未找到好的解决方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataList[indexPath.row];
    NSInteger orderStatus = [dic[@"order_status"] integerValue];
    if (orderStatus == 0) {  //待付款
        OrderWaitPayVC *vc = [[OrderWaitPayVC alloc] init];
        vc.productID = dic[@"id"];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        OrderDetailVC *vc = [[OrderDetailVC alloc] init];
        vc.productID = dic[@"id"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}*/

#pragma CellClickDetegate

- (void)reloadList {
    [self fetchData];
}

- (void)cancelOrderWithOrder:(NSDictionary *)order {
    NSString *url = @"/api/tiny-shop/v1/member/order/close";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"id"] = order[@"id"];
    [[NetWorksTools shared] requestWithMethod:GET url:url params:params successBlock:^(NSDictionary * _Nonnull response) {
        [self fetchData];
    } failureBlock:^(NSError * _Nonnull error) {

    }];
}

- (void)deleteWithOrder:(NSDictionary *)order {
    NSString *url = [NSString stringWithFormat:@"/api/tiny-shop/v1/member/order/delete?id=%@", order[@"id"]];
    [[NetWorksTools shared] requestWithMethod:DELETE url:url params:nil successBlock:^(NSDictionary * _Nonnull response) {
        [self fetchData];
    } failureBlock:^(NSError * _Nonnull error) {

    }];
}

- (void)toPayWithOrder:(NSDictionary *)order {
//    OrderPayVC *vc = [[OrderPayVC alloc] init];
//    vc.orderType = [order[@"order_type"] integerValue];
//    vc.orderID = order[@"id"];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)orderDetailWithOrder:(NSDictionary *)order {
//    NSInteger orderStatus = [order[@"order_status"] integerValue];
//    if (orderStatus == 0 || orderStatus == -4 || orderStatus == 101 || orderStatus == 104 || orderStatus == 105) {  //待付款
//        OrderWaitPayVC *vc = [[OrderWaitPayVC alloc] init];
//        vc.productID = order[@"id"];
//        [self.navigationController pushViewController:vc animated:YES];
//    } else {
//        OrderDetailVC *vc = [[OrderDetailVC alloc] init];
//        vc.productID = order[@"id"];
//        [self.navigationController pushViewController:vc animated:YES];
//    }
}

#pragma OrderRepairCellDelegate

- (void)gotoOrderRepairDetailWithOrder:(NSDictionary *)order {
//    OrderRepairDetailVC *vc = [[OrderRepairDetailVC alloc] init];
//    vc.productID = order[@"id"];
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma DZNEmptyDataSetSource, DZNEmptyDataSetDelegate

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSDictionary* attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName:Color999};
    return[[NSAttributedString alloc]initWithString:@"暂无订单" attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"order_img_meiyou"];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100;
}

@end
